<template>
  <div class="wrap" style="padding: 15px">
    <el-row>
      <el-form :inline="true" :model="queryParams" class="demo-form-inline">
        <el-form-item label="手机号">
          <el-input v-model="queryParams.phone" clearable placeholder="手机号"></el-input>
        </el-form-item>
        <el-form-item label="赛事名称">
          <el-input v-model="queryParams.gameName" clearable placeholder="赛事名称"></el-input>
        </el-form-item>


        <el-form-item>
          <el-button type="primary" @click="getData">查询</el-button>
          <el-button type="primary" @click="exportData" v-if="queryParams.gameId">数据导出</el-button>

        </el-form-item>
      </el-form>
    </el-row>

    <el-table :data="tableData" style="width: 100%" stripe v-loading="loading">
      <el-table-column label="姓名" prop="name"></el-table-column>
      <el-table-column label="手机号" prop="phone"></el-table-column>
      <el-table-column label="赛事名称" prop="gameName"></el-table-column>
      <el-table-column label="赛事项目" prop="gameItem"></el-table-column>
      <el-table-column label="价格" prop="price"></el-table-column>
      <el-table-column label="提交时间" prop="createTime"></el-table-column>
    </el-table>
    <div class="block" style="text-align: right">
      <el-pagination @size-change="getData" @current-change="getData" :current-page.sync="queryParams.page"
        :page-sizes="[10, 20, 30, 40]" :page-size.sync="queryParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
    </div>
    <List ref="list" />
  </div>
</template>
<script>
import List from './list.vue'
import { gameOrderPageList, gameDel, exportGameOrder } from '@/config/api'
import { times, divide } from '@/utils/tools';

export default {
  components: {
    List,
  },
  data() {
    return {
      queryParams: {
        phone: undefined,
        page: 1,
        pageSize: 10,
        gameName: undefined,
        gameId: undefined
      },
      tableData: [

      ],
      total: 0,
      loading: false,


    };
  },

  created() {
    this.$route.query.gameName && (this.queryParams.gameName = this.$route.query.gameName)
    this.$route.query.id && (this.queryParams.gameId = this.$route.query.id)

    this.getData()
  },
  methods: {
    // 新增
    add() {
      this.$refs.detail.addShow()
    },
    // 新增赛事
    addCompetition() {
      this.$router.push("/sports/addCompetition")
    },
    edit(recore) {
      this.$router.push("/sports/addCompetition?id=" + recore.id)
    },
    getData() {
      gameOrderPageList(this.queryParams).then(res => {
        res.data.result.list.map(item => {
          item.price = divide(item.price, 100)
        })
        this.tableData = res.data.result.list
        this.total = res.data.result.total
      })
    },
    // 选择报名表

    selectApply(e) {
      this.$refs.list.show(e.id)
    },
    // 数据导出
    exportData() {
      exportGameOrder({ gameId: this.queryParams.gameId }).then(res => {
        const fileName = "报名表.xlsx";
        if ("download" in document.createElement("a")) {
          // 非IE下载
          const blob = new Blob([res.data], { type: "application/ms-excel" });
          const elink = document.createElement("a");
          elink.download = fileName;
          elink.style.display = "none";
          elink.href = URL.createObjectURL(blob);
          document.body.appendChild(elink);
          elink.click();
          URL.revokeObjectURL(elink.href); // 释放URL 对象
          document.body.removeChild(elink);
        }
      })
    },
    del(id) {
      this.$confirm('此操作将删除该赛事, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        gameDel({ id }).then(res => {
          if (res.data.status == 200) {
            this.getData()
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }

        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
  },
};
</script>